package yxy.cherry.battle.module.affix.buff;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.battle.module.PropertyType;
import yxy.cherry.battle.module.affix.Buff;
import yxy.cherry.battle.module.affix.AffixFactory.BuffTypeEnum;
import yxy.cherry.battle.module.affix.BuffType;
import yxy.cherry.battle.module.record.stream.RecordStream;
import yxy.cherry.battle.module.record.stream.RecordStream_BFA;
import yxy.cherry.battle.module.record.stream.RecordStream_BUF;
import yxy.cherry.data.bean.skill;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>魔张角被动技能-神雷</p>
 * <P>当魔张角在场的时候,初始会给所有敌方武将加神雷buff，在敌方武将身上有麻痹效果时触发神雷buff减少攻击力<p/>
 **/
@BuffType(Type = BuffTypeEnum.神雷)
public class Affix_82_GodThunder extends Buff  {

    public Affix_82_GodThunder(Fighter source, Fighter target, skill skilldata, int[] params) {
        super(source, target, skilldata, params);
    }
    @Override
    public boolean exec(Fighter source, Fighter target) {
        boolean isParalysis = isParalysis(source,target);
        // 计算神雷buff的减攻属性
        if (isParalysis){
            setReduceAttackData(target);
        }else {
            clearReduceAttackData(target);
        }
        return isParalysis;
    }

    /**
     * 判断目标武将是否有麻痹buff
     * 判断源武将是否还存活
     */
    private boolean isParalysis(Fighter source,Fighter target){
        //判断魔张角是否存活,死了就不触发光环
        if (!source.is活着())return false;
        Map<String, Buff> buffMap = target.getBuffMap();
        for (String buffCode : buffMap.keySet()) {
            //目标武将身上是否有麻痹buff
            if (buffMap.get(buffCode) instanceof Affix_55_麻痹){
                return true;
            }
        }
        return false;
    }

    /**
     * 设置减攻属性
     */
    private void  setReduceAttackData(Fighter target){
        //获取buff的属性
        PropertyData propertyData = getPropertyData();
        if(propertyData == null){
            propertyData = new PropertyData();
            setPropertyData(propertyData);
        }
        //获取要减少攻击的武将属性
        long value = target.getData().getPropertyData().getValue(PropertyType.P_攻击);
        //获取神雷技能减少攻击的的万分比
        int[] param = getSkilldata().getParam();
        int reduceAttackProportion = param[1];
        //计算需要减少的攻击
        long reduceAttackNumber = (value * reduceAttackProportion) / VALUE_比率分母;
        propertyData.addValue(PropertyType.P_攻击,(~(reduceAttackNumber - 1)));

        Battle.battleLogger(6,"神雷光环：武将"+target.getName()
                +"#减少"+reduceAttackNumber+"攻击力"
                +"#原始攻击为："+value
                +"#减少后的攻击力为："+(value+reduceAttackNumber));
    }

    /**
     * 清空减攻属性
     */
    private void clearReduceAttackData(Fighter target){
        //获取buff的属性
        PropertyData propertyData = getPropertyData();
        if(propertyData != null){
            //判断之前是否有触发过狱火buff
            long value = propertyData.getValue(PropertyType.P_攻击);
            if(value < 0){
                List<Buff> buffList = new ArrayList<>();
                buffList.add(this);
                //发给前端清空buff标识
                getRecord().stream(new RecordStream_BFA(target, buffList)).record();
            }
            propertyData.setValue(PropertyType.P_攻击,0);
        }
    }

    @Override
    public RecordStream record() {
        return getRecord().stream(new RecordStream_BUF(getActor(), getActor().getBuffMap().get(this.getBuffCodeSeq())));
    }
}
